<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Soonpage\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Application\Model\Invitation;
use Application\Model\InvitationTable;
use Application\Model\Contact;
use Application\Model\ContactTable;

class IndexController extends AbstractActionController
{
	protected $MailService;
	protected $invitationTable;
	protected $contactTable;

    public function indexAction()
    {
        return array();
    }

    public function quiAction()
    {
        return array();
    }

    public function contactAction()
    {
        return array();
    }

    public function AjouterInvitationAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			$reponse = false;

        	$contactmail = json_decode(file_get_contents('php://input'), true);

			/* préparation des datas avant insertion en base */
			$invitation = new Invitation();
			$invitation->exchangeArray($contactmail);

			if( !$this->getInvitationTable()->isMailInvitationDejaEnBase ($invitation) ) {
				$result = $this->getInvitationTable()->saveInvitation ($invitation);

				if( (int)$result > 0 ) {
					$reponse = true;
				} else {
					$reponse = false;
				}

				echo json_encode( $reponse );
			} else {
				echo 'pris';
			}

		}
		
		return $this->response;
	}

	public function TraiterFormulaireContactAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			$reponse = false;

        	$contactmail = json_decode(file_get_contents('php://input'), true);

			/* préparation des datas avant insertion en base */
			$contact = new Contact();
			$contact->exchangeArray($contactmail);

			if( !$this->getContactTable()->isDemanceContactDejaEnBase ($contact) ) {
				$config = $this->getServiceLocator()->get('Config');
				$result = $this->getContactTable()->saveDemandeFormulaireDeContact ($contact);

				if( (int)$result > 0 ) {
					$reponse = true;

					/* envoie du mail */
					$this->getMailService()->envoyer_demande_contact ( $contact, $config['config_vars']['contact'] );

				} else {
					$reponse = false;
				}

				echo json_encode( $reponse );
			} else {
				echo 'pris';
			}

		}
		
		return $this->response;
	}

	/*********************************** MODELS SERVICES ********************************************/


	public function getInvitationTable()
	{
		if( !$this->invitationTable ){
			$sm = $this->getServiceLocator();
			$this->invitationTable = $sm->get("Application\Model\InvitationTable");
		}
		return $this->invitationTable;
	}

	public function getContactTable()
	{
		if( !$this->contactTable ){
			$sm = $this->getServiceLocator();
			$this->contactTable = $sm->get("Application\Model\ContactTable");
		}
		return $this->contactTable;
	}

	public function getMailService()
	{
		if( !$this->MailService ){
			$sm = $this->getServiceLocator();
			$this->MailService = $sm->get("MailService");
		}

		return $this->MailService;
	}

}
